#include "stm32f4xx.h"
#include "led.h"

#define KEY_PRESSED     0x00
#define KEY_NOT_PRESSED 0x01

CAN_InitTypeDef        CAN_InitStructure;
CAN_FilterInitTypeDef  CAN_FilterInitStructure;
CanTxMsg TxMessage;
uint8_t ubKeyNumber = 0x0;

static void NVIC_Config(void);
static void CAN_Config(void);
static void Delay(void);
void Init_RxMes(CanRxMsg *RxMessage);

/*typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
__IO uint32_t ret = 0;  for return of the interrupt handling
volatile TestStatus TestRx;

TestStatus CAN_Polling(void);*/

int main(void){
	NVIC_Config();
	LED_Init();
	CAN_Config();
	LED_On(LED4);

	while(1){
		while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10) == KEY_PRESSED){
			if(ubKeyNumber == 0x4){
		        ubKeyNumber = 0x00;
		      }
			else{
				LED_Display(++ubKeyNumber);
		        TxMessage.Data[0] = ubKeyNumber;
		        CAN_Transmit(CAN1, &TxMessage);
		        Delay();
		        while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10) == KEY_NOT_PRESSED){
		        }
		      }
		}
	}
}

static void CAN_Config(void){
	GPIO_InitTypeDef  GPIO_InitStructure;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
	CAN_DeInit(CAN1);

	CAN_InitStructure.CAN_ABOM = DISABLE;
	CAN_InitStructure.CAN_AWUM = DISABLE;
	CAN_InitStructure.CAN_NART = DISABLE;
	CAN_InitStructure.CAN_RFLM = DISABLE;
	CAN_InitStructure.CAN_TTCM = DISABLE;
	CAN_InitStructure.CAN_TXFP = DISABLE;
	CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
	CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

	/* CAN Baudrate = 1Mbps (CAN clocked at 42 MHz) */
	CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
	CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
	CAN_InitStructure.CAN_Prescaler = 2;
	CAN_Init(CAN1, &CAN_InitStructure);

	/* CAN filter init */
	CAN_FilterInitStructure.CAN_FilterNumber = 0;
	CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
	CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
	CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
	CAN_FilterInitStructure.CAN_FilterIdLow =0x0000;
	CAN_FilterInitStructure.CAN_FilterIdHigh =0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
	CAN_FilterInit(&CAN_FilterInitStructure);

	/* transmit */
    TxMessage.StdId = 0x321;  //the StdId value can not be larger than 0x7ff
    TxMessage.ExtId = 0x01;
	TxMessage.RTR = CAN_RTR_DATA;  //Data Frame,Remote disabled
	TxMessage.IDE = CAN_ID_STD;    //Standard ID enabled
	TxMessage.DLC = 1;             //1 Byte data to be transmitted

	/* Enable FIFO 0 message pending Interrupt */
	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}

static void NVIC_Config(void){
	NVIC_InitTypeDef  NVIC_InitStructure;

	NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}


/**Initializes the Rx Message.
 * RxMessage: pointer to the message to initialize**/
void Init_RxMes(CanRxMsg *RxMessage){
	uint8_t ubCounter = 0;

	RxMessage->StdId = 0x00;
	RxMessage->ExtId = 0x00;
	RxMessage->IDE = CAN_ID_STD;
	RxMessage->DLC = 0;
	RxMessage->FMI = 0;
	for (ubCounter = 0; ubCounter < 8; ubCounter++){
	RxMessage->Data[ubCounter] = 0x00;
	}
}

void LED_Display(uint8_t Ledstatus){
  /* Turn off all leds */
	LED_Off(LED0);
	LED_Off(LED1);
	LED_Off(LED2);
	LED_Off(LED3);

	switch(Ledstatus){
	case(1):
		LED_On(LED0);
	  break;

	case(2):
		LED_On(LED1);
	  break;

	case(3):
		LED_On(LED2);
	  break;

	case(4):
		LED_On(LED3);
	  break;
	default:
	  break;
	}
}

static void Delay(void){
  uint16_t nTime = 0x0000;
  for(nTime = 0; nTime <0xFFF; nTime++){
  }
}

